package com.vv.tools.common.constant.users;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * @author wen.li
 */
@Getter
@AllArgsConstructor
public enum RoleOperationEnum {

    CREATE(8, "create", "1000"),
    RETRIEVE(4, "retrieve", "0100"),
    UPDATE(2, "update", "0010"),
    DELETE(1, "delete", "0001");

    private final int code;

    private final String name;

    private final String desc;

    public static List<String> parseName(int code) {
        List<String> nameList = new ArrayList<>();
        for (RoleOperationEnum value : values()) {
            if ((value.getCode() & code) > 0) {
                nameList.add(value.getName());
            }
        }
        return nameList;
    }

    public static int parseCode(List<String> nameList) {
        int code = 0;
        for (String name : nameList) {
            RoleOperationEnum roleOperationEnum = byName(name);
            if (null != roleOperationEnum) {
                code += roleOperationEnum.code;
            }
        }
        return code;
    }

    public static RoleOperationEnum byName(String name) {
        return Arrays.stream(values()).filter(o -> o.getName().equals(name)).findFirst().orElse(null);
    }

}
